package com.cplatform.movie.back.utils;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import com.hisun.crypt.mac.CryptUtilImpl;

public class Md5Util {
  
  /** MD5 解密验签*/
  public static boolean MD5Verify(String srcData, String hmac, String key) {
    String _crpy = MD5Sign(srcData, key);
    return _crpy.equals(hmac);
  }
  
  
	/** MD5 加密 */
	public static String MD5Sign(String signData, String signkey) {
		CryptUtilImpl impl = new CryptUtilImpl();
		String value = impl.cryptMd5(signData, "");
		String value2 = impl.cryptMd5(value, signkey);

		return value2;
	}
	
	public static String getValue(String respMsg, String name) {
		String[] resArr = StringUtils.split(respMsg, "&");

		Map resMap = new HashMap();
		for (int i = 0; i < resArr.length; i++) {
			String data = resArr[i];
			int index = StringUtils.indexOf(data, '=');
			String nm = StringUtils.substring(data, 0, index);
			String val = StringUtils.substring(data, index + 1);

			resMap.put(nm, val);
		}

		return (String) resMap.get(name) == null ? "" : (String) resMap.get(name);
	}
  
	/**
	 * 获取请求ip地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {
		if (null == request) {
			return null;
		}

		String proxs[] = { "X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_CLIENT_IP", "HTTP_X_FORWARDED_FOR" };
		String ip = null;

		for (String prox : proxs) {
			ip = request.getHeader(prox);
			if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
				continue;
			} else {
				break;
			}
		}

		if (StringUtils.isBlank(ip)) {
			return request.getRemoteAddr();
		}
		return ip;
	}
  
	
	public static void main(String[] args) {
		String s = "70015690661022159712551187";
		String sign = Md5Util.MD5Sign(s, "aRJVS8dur4JJkn6e");
		Boolean vsign = Md5Util.MD5Verify(s, sign, "aRJVS8dur4JJkn6e");
		System.out.println(vsign);
	}
  
}